<?php
session_start(); //we need to call PHP's session object to access it through CI
class soal extends CI_Controller {

	function __construct()
	{
		parent::__construct();
	}

	function index()
	{
		$params = array('tablename' => 'soal', 'primary_key' => 'so_id');
		$this->load->library('grid', $params);
		$this->grid->add_subject('Siswa');
		$this->grid->add_field('so_desc', 'Isi Soal', '', 'show');
		$this->grid->add_field('uj_id', 'Nama Ujian', '', 'show');
		$this->grid->set_menu('soal');
		$this->grid->set_portlet_style('portlet box yellow');
		$this->grid->set_portlet_title('Data Soal');
		$this->grid->show();
	}

	function insert(){
		$this->load->model("m_soal");
		$value['uj_id'] = $this->input->post('uj_id');
		$value['so_desc'] = $this->input->post('so_desc');
		if(isset($_FILES['so_pict'])){
			$value['so_pict'] = $_FILES['so_pict']['name'];
			if(strlen($value['so_pict'])) $this->upload_image(); 
		}
		$value['so_id'] = $this->m_soal->insert($value);
		$value['ja_desc'] = $this->input->post('ja_desc');
		$this->submit_jawaban($value);
		echo json_encode(array($value));
	}
	
	function upload_image(){
		$name = $_FILES['so_pict']['name'];
		$size = $_FILES['so_pict']['size'];
		$valid_formats = array("jpg", "png", "gif", "bmp");
		list($txt, $ext) = explode(".", $name);
		if(strlen($name)<=50){
			if(in_array($ext,$valid_formats)){
				$tmp = $_FILES['so_pict']['tmp_name'];
				if(move_uploaded_file($tmp, IMAGE_PATH.$name)) return array('uploaded'=>'Success');
				else return array('uploaded'=>'Failed');
			}else
				return array('uploaded'=>'Format gambar tidak diperbolehkan');
		}else return array('uploaded'=>'Nama file terlalu panjang');
	}
	
	function reset_soal(){
		$_SESSION['ujian']['soal'] = array();
		$this->load->model("m_soal");
		echo $this->m_soal->get_last_id();
	}
	
	function get_soal($id){
		$this->load->model("m_soal");
		$this->load->model("m_jawaban");
		$soals = $this->m_soal->get_soal_with_jawaban_by_id($id);
		$i = 1;
		$value = array();
		foreach($soals as $soal){
			$value['ja_desc'.$i] = $soal->ja_desc;
			$value['so_id'] = $soal->so_id;
			$value['so_desc'] = $soal->so_desc;
			if($soal->so_pict) $value['so_pict'] = 'images/uploaded/'.$soal->so_pict;
			else $value['so_pict'] = '';
			if($soal->ja_stat == 1) $value['ja_stat'] = $i;
			$i++;
		}
		echo json_encode($value);
	}
	
	function submit_jawaban($data){
		$i = 1;
		$this->load->model('m_jawaban');
		$this->m_jawaban->delete(array('so_id' => $data['so_id']));
		foreach ($data["ja_desc"] as $key => $item){
			$value['ja_desc'] = $item;
			$value['so_id'] = $data['so_id'];
			if($i == 1) $value['ja_stat'] = 1;
			else $value['ja_stat'] = 0;
			$this->m_jawaban->insert($value);
			$i++;
		}
	}
	
	function delete(){
		$so_id = $this->input->post('so_id');
		$this->load->model("m_soal");
		$this->m_soal->delete(array('so_id' => $so_id));
	}
	
	function update(){
		$this->load->model("m_soal");
		$value['so_desc'] = $this->input->post('so_desc');
		$value['so_id'] = $this->input->post('so_id');
		if(isset($_FILES['so_pict'])){
			$value['so_pict'] = $_FILES['so_pict']['name'];
			if(strlen($value['so_pict'])) $this->upload_image(); 
		}
		$this->m_soal->update($value, array('so_id' => $value['so_id']));
		$value['ja_desc1'] = $this->input->post('ja_desc1');
		$value['ja_desc2'] = $this->input->post('ja_desc2');
		$value['ja_desc3'] = $this->input->post('ja_desc3');
		$value['ja_desc4'] = $this->input->post('ja_desc4');
		$value['ja_desc5'] = $this->input->post('ja_desc5');
		$value['ja_stat'] = $this->input->post('ja_stat');
		$value['ja_correct'] = $this->input->post('ja_desc'.$value['ja_stat']);
		$this->submit_jawaban($value);
		echo json_encode(array($value));
	}

}
?>